package com.wdcg.activity;

import java.util.ArrayList;

import com.wdcg.R;
import com.wdcg.model.access.BonusAccess;
import com.wdcg.model.access.DwarfAccess;
import com.wdcg.model.dao.DwarfDescription;
import com.wdcg.utils.GameLogic;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class QuizActivity extends Activity {
	
	public final static String PARAM_DWARF_ID = "DwarfID";
	ImageView imageView;
	Button answerButton_1;
	Button answerButton_2;
	Button answerButton_3;
	Button answerButton_4;
	DwarfDescription dwarfDesc;
	TextView questionTitleLabel;
	RelativeLayout rl;
	ArrayList<DwarfDescription> invalidValues;
	ArrayList<Button> buttons;
	Button validAnswerButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(getString(R.string.quiz_game_title));
		setContentView(R.layout.quiz_game);
		rl = (RelativeLayout) findViewById(R.id.quiz_main_layout);

		questionTitleLabel = new TextView(this);

		dwarfDesc = DwarfAccess.getDwarfDescriptionForDwarfId(getIntent()
				.getIntExtra(PARAM_DWARF_ID, -1));
		
		//create imageView
		final Bitmap SOURCE_BITMAP = BitmapFactory.decodeResource(
				getResources(),
				getResources().getIdentifier(
						"@drawable/" + dwarfDesc.getDwarf().getImgTitle(),
						null, getPackageName()));
		imageView = new ImageView(this);
		imageView.setImageBitmap(SOURCE_BITMAP);

		//create buttons 
		answerButton_1=new Button(this);
		answerButton_2=new Button(this);
		answerButton_3=new Button(this);
		answerButton_4=new Button(this);
		
		buttons = new ArrayList<Button>();
		buttons.add(answerButton_1);
		buttons.add(answerButton_2);
		buttons.add(answerButton_3);
		buttons.add(answerButton_4);

		//questionTitleLabel
		this.questionTitleLabel=new TextView(this);
		this.questionTitleLabel.setText(getString(R.string.quiz_game_question));
		this.questionTitleLabel.setTextSize(25);
		this.questionTitleLabel.setGravity(android.view.Gravity.CENTER_VERTICAL);
		//layout
		ViewTreeObserver observer = rl.getViewTreeObserver();
		observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			public void onGlobalLayout() {
				int[] bestImageSize = getBestSize(rl.getWidth(), rl.getHeight());
				//image view
				if (bestImageSize[1] > rl.getHeight() * .5) {
					bestImageSize[1] = (int) (rl.getHeight() * .5);
				}
				LayoutParams lp = new LayoutParams(bestImageSize[0],
						bestImageSize[1]);
				lp.leftMargin = (int) ((rl.getWidth() - bestImageSize[0]) * .5);
				lp.topMargin = (int) ((rl.getHeight() - bestImageSize[1]) * .5);

				int imgY=lp.topMargin;
				int imgH=lp.height;
				imageView.setLayoutParams(lp);
				
				// question title
				lp=new LayoutParams(rl.getWidth()-20,imgY);
				lp.topMargin=0;
				lp.leftMargin=10;
				questionTitleLabel.setLayoutParams(lp);
				
				//buttons
				lp=new LayoutParams(rl.getWidth()-20,rl.getHeight());
				int buttonH=(rl.getHeight()-imgY-imgH)/2;
				int buttonY=rl.getHeight()-2*buttonH;
				int buttonX=0;
				int buttonW=rl.getWidth()/2;
				
				//b1
				lp = new LayoutParams(buttonW,buttonH);
				lp.leftMargin = (int) buttonX;
				lp.topMargin = (int) buttonY;
				answerButton_1.setLayoutParams(lp);
				//b2
				buttonX=buttonW;
				lp = new LayoutParams(buttonW,buttonH);
				lp.leftMargin = (int) buttonX;
				lp.topMargin = (int) buttonY;
				answerButton_2.setLayoutParams(lp);
				//b3
				buttonY+=buttonH;
				buttonX=0;
				lp = new LayoutParams(buttonW,buttonH);
				lp.leftMargin = (int) buttonX;
				lp.topMargin = (int) buttonY;
				answerButton_3.setLayoutParams(lp);
				
				//b4
				buttonX=buttonW;
				lp = new LayoutParams(buttonW,buttonH);
				lp.leftMargin = (int) buttonX;
				lp.topMargin = (int) buttonY;
				answerButton_4.setLayoutParams(lp);
			}
		});
		
		rl.addView(imageView);
		rl.addView(answerButton_1);
		rl.addView(answerButton_2);
		rl.addView(answerButton_3);
		rl.addView(answerButton_4);
		rl.addView(questionTitleLabel);
		
		invalidValues=DwarfAccess.getRandomDwarvsForQuiz(3, this.dwarfDesc.getDwarf());
		this.setButtons();
	}

	public int[] getBestSize(int w, int h) {
		int min = w < h ? w : h;
		return new int[] { min, min };
	}

	public int[] displaySize() {

		int width = imageView.getWidth();
		int height = imageView.getHeight();
		return new int[] { width, height };
	}

	public void setButtons(){
		//first, set text
		ArrayList<Button> randomButtons = new ArrayList<Button>(buttons);
		int next=0;
		while(randomButtons.size()>1){
			int random=(int)Math.random()*randomButtons.size();
			Button button=randomButtons.get(random);
			randomButtons.remove(random);
			
			button.setText(invalidValues.get(next++).getName());
		}
		validAnswerButton=randomButtons.get(0);
		validAnswerButton.setText(this.dwarfDesc.getName());
		//second, set listeners
		
		for(Button button:buttons){
			button.setOnClickListener(new Button.OnClickListener(){
				
				public void onClick(View v) {
					final AlertDialog alertDialog = new AlertDialog.Builder(v.getContext()).create();

					if(v==validAnswerButton){
						alertDialog.setTitle(getString(R.string.quiz_game_success_alert_title));
						alertDialog.setMessage(getString(R.string.quiz_game_success_alert_message));
						alertDialog.setButton(Dialog.BUTTON_NEUTRAL,getString(R.string.game_alert_close_button), new DialogInterface.OnClickListener(){
							public void onClick(DialogInterface dialog, int which) {
								alertDialog.dismiss();
								GameLogic.setDwarfUnlocked(dwarfDesc.getDwarf());
								GameLogic.getMoreDwarfsVisible();
								finish();
							}
						});
						
					}else{
						alertDialog.setTitle(getString(R.string.quiz_game_fail_alert_title));
						alertDialog.setMessage(getString(R.string.quiz_game_fail_alert_message));
						alertDialog.setButton(Dialog.BUTTON_NEUTRAL,getString(R.string.game_alert_close_button), new DialogInterface.OnClickListener(){
							public void onClick(DialogInterface dialog, int which) {
								alertDialog.dismiss();	
								finish();
							}
						});
					}
					alertDialog.show();

				}
		
			});
		}
	}
	
}
